package com.weirdor.mongo.exception;

import com.weirdor.mongo.exception.code.BaseErrorCode;
import com.weirdor.mongo.exception.code.DefaultHttpErrorCode;

/**
 * @Author werdor
 * @Date 2021/11/8 3:51 下午
 **/
public abstract class BaseException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    // 设置默认值，默认是服务端错误，否则调用getErrorCode()可能会导致空指针异常
    protected BaseErrorCode errorCode = DefaultHttpErrorCode.SERVER_ERROR;

    /**
     * 用带 {@link BaseErrorCode} 的构造器替代
     *
     * @param message
     */
    @Deprecated
    public BaseException(String message) {
        super(message);
    }

    /**
     * 用带 {@link BaseErrorCode} 的构造器替代
     *
     * @param throwable
     */
    @Deprecated
    public BaseException(Throwable throwable) {
        super(throwable);
    }

    /**
     * 用带 {@link BaseErrorCode} 的构造器替代
     *
     * @param message
     * @param throwable
     */
    @Deprecated
    public BaseException(String message, Throwable throwable) {
        super(message, throwable);
    }

    // -------------------------------------------------------------------------------------------------

    /**
     * 构建ZuihuiyouException，将用errorCode的描述信息作为异常的描述信息
     *
     * @param errorCode
     *            错误码
     */
    public BaseException(BaseErrorCode errorCode) {
        // 未对参数验证，虽可能存在空指针异常，即使存在也是我们内部问题，下同
        this(errorCode, errorCode.toString());
    }

    public BaseException(BaseErrorCode errorCode, String excMsg) {
        super(excMsg);
        this.errorCode = errorCode;
    }

    public BaseException(Throwable throwable, BaseErrorCode errorCode) {
        this(throwable, errorCode, errorCode.toString());
    }

    public BaseException(Throwable throwable, BaseErrorCode errorCode, String excMsg) {
        super(excMsg, throwable);
        this.errorCode = errorCode;
    }

    public int getErrorCode() {
        return errorCode.getCode();
    }
}
